package com.ficus.road.maintain.handler.monitor;

import com.ficus.road.maintain.dto.Checklist;
import com.ficus.road.maintain.util.MyDateUtil;
import com.ficus.road.maintain.util.excel.ExcelUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author xiawen yang
 * @date 2022/5/23 下午7:28
 */
@Component
@Order(5)
@RequiredArgsConstructor
@Slf4j
public class ExportChecklistHandler extends AbstractMonitorHandler {

    @Value("${monitor.dirPath}")
    private String dirPath;

    @Override
    Checklist doFilter(Checklist fetch) {
        if (ObjectUtils.isEmpty(fetch)) {
            return fetch;
        }
        Date date = new Date();
        int dayOfMonth = MyDateUtil.getDayOfMonth(date);
        String dateStr = MyDateUtil.dateFormatStr(date, "yyyy-MM-dd");
        String filePath = dirPath + String.format("Checklist-%s.xlsx", dateStr);
        ExcelUtil.exportCollections2Excel(dirPath, filePath, fetch, "carList", dayOfMonth);
        ExcelUtil.exportObject2Excel(dirPath, filePath, fetch.getPushStatistic(), dayOfMonth);
        log.info("数据成功写入excel文件");
        return fetch;
    }
}
